package com.zy.teacherSide.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zy.studentSide.entity.StudentClassroom;
import com.zy.studentSide.service.IStudentClassroomService;
import com.zy.teacherSide.entity.Classroom;
import com.zy.teacherSide.entity.Course;
import com.zy.teacherSide.entity.dto.CourseDTO;
import com.zy.teacherSide.entity.dto.StudentCourseDTO;
import com.zy.teacherSide.entity.ov.CourseOV;
import com.zy.teacherSide.service.IClassroomService;
import com.zy.teacherSide.service.ICourseService;
import com.zy.util.R;
import com.zy.util.RandomNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 29787
 */
@RestController
public class CourseController {

    @Autowired
    private ICourseService iCourseService;

    @Autowired
    private IClassroomService ics;


    /**
     * 教师创建课程
     * @param cov
     * @param teacherId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherCreateCourse/{teacherId}")
    public R createCourse(@RequestBody CourseOV cov, @PathVariable Integer teacherId){
        //根据班级名称和教师id查询班级id
        Classroom c = ics.getClassroomByNameAndTeacherId(cov.getClassroomName(), teacherId);
        //根据查询出的班级id和教师id和课程名称校验是否重复
        Course course = iCourseService.getCourse(cov.getCourseName(), teacherId, c.getClassroomId());
        //校验课程是否已经存在 course == null表示该课程为创建，否则为已创建
        if (course == null) {
            //封装课程数据，存入数据库
            Course c1 = new Course();
            Integer id = RandomNum.getRandomID(6);
            c1.setCourseId(id);
            c1.setCourseName(cov.getCourseName());
            c1.setClassroomId(c.getClassroomId());
            c1.setCredit(cov.getCredit());
            c1.setTeacherId(teacherId);
            //将该课程信息存入数据库，同时返回前端
            return new R(iCourseService.save(c1),"success");
        }else{
            return new R(false,"err");
        }

    }

    /**
     * 获取当前老师的所有课程的课程名称
     * @param teacherId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/getAllCourseNameByTeacherId/{teacherId}")
    public R getAllClassroomByTeacherId(@PathVariable Integer teacherId) {
        List<Course> allCourse = iCourseService.getAllCourse(teacherId);
        List<CourseOV> list = new ArrayList<CourseOV>();
        for (Course c : allCourse) {
            CourseOV cov = new CourseOV();
            cov.setCourseName(c.getCourseName());
            list.add(cov);
        }
        return new R(true,list);
    }

    /**
     * 教师分页查询课程数据
     * @param currentPage
     * @param pageSize
     * @param teacherId
     * @return R
     */
    @GetMapping
    @RequestMapping(value = "/teacherGetCourseByPage/{currentPage}/{pageSize}/{teacherId}")
    public R getStudentByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@PathVariable Integer teacherId){
        IPage<Course> page = iCourseService.getPage(currentPage, pageSize, teacherId);

        CourseDTO cd = new CourseDTO();
        cd.setCurrentPage((int)page.getCurrent());
        cd.setTotal((int)page.getTotal());
        cd.setPageSize((int)page.getSize());

        List list = page.getRecords();
        List<CourseOV> co = new ArrayList<>();

        for(int i = 0; i < list.size();i++){
            Course c = (Course)list.get(i);
            CourseOV cov = new CourseOV();

            Classroom c1 = ics.getClassroomByClassroomId(c.getClassroomId());

            cov.setClassroomName(c1.getClassroomName());
            cov.setCourseName(c.getCourseName());
            cov.setCredit(c.getCredit());
            co.add(cov);
        }
        cd.setCourseOV(co);
        return new R(true,cd);
    }

    /**
     * 教师编辑课程信息
     * @param cov
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherUploadCourse/{teacherId}")
    public R uploadCourse(@RequestBody CourseOV cov,@PathVariable Integer teacherId){
        Course course = new Course();
        Classroom cr = ics.getClassroomByNameAndTeacherId(cov.getClassroomName(), teacherId);
        Course c = iCourseService.getCourseByCourseNameAndTeacherId(cov.getCourseName(), teacherId);
        course.setId(c.getId());
        course.setClassroomId(cr.getClassroomId());
        course.setCourseId(c.getCourseId());
        course.setCourseName(cov.getCourseName());
        course.setCredit(cov.getCredit());
        course.setTeacherId(teacherId);
        return new R(iCourseService.updateById(course));
    }

    /**
     * 教师删除课程信息
     * @param cov
     * @param teacherId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/teacherDeleteCourse/{teacherId}")
    public R teacherDeleteCourse(@RequestBody CourseOV cov,@PathVariable Integer teacherId){
        Classroom c = ics.getClassroomByNameAndTeacherId(cov.getClassroomName(), teacherId);
        Course course = iCourseService.getCourse(cov.getCourseName(), teacherId, c.getClassroomId());
        return new R(iCourseService.removeById(course.getId()));
    }

}
